/**
 *  Copyright 2002-2009 the original author or authors.
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package foo.bar.blog.web.action.general;

import com.google.code.kaptcha.Constants;
import foo.bar.blog.core.services.UserService;
import foo.bar.blog.core.services.exception.UserAndEmailCombinationDoesNotExistException;
import foo.bar.blog.web.action.BaseActionBean;
import net.sourceforge.stripes.action.*;
import net.sourceforge.stripes.integration.spring.SpringBean;
import net.sourceforge.stripes.validation.EmailTypeConverter;
import net.sourceforge.stripes.validation.LocalizableError;
import net.sourceforge.stripes.validation.Validate;
import net.sourceforge.stripes.validation.ValidationMethod;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.perf4j.aop.Profiled;

/**
 * Actions dealing with forgotten password 
 *
 * @author tmjee
 * @version $Date: 2009-03-14 06:53:07 +0000 (Sat, 14 Mar 2009) $ $Id$
 */
public class ForgottenPasswordActionBean extends BaseActionBean {

    private static final Log LOG = LogFactory.getLog(ForgottenPasswordActionBean.class);

    public static final String VIEW = "general/forgottenPassword.jsp";

    @Validate(required = true, trim = true, on={"handleForgottenPassword"})
    private String username;

    @Validate(required = true, trim = true, converter = EmailTypeConverter.class, on={"handleForgottenPassword"})
    private String email;

    @Validate(required = true, trim = true, on={"handleForgottenPassword"})
    private String captcha;



    @DefaultHandler
    @Profiled(tag="ForgottenPasswordActionBean_view", logFailuresSeparately = true)
    public Resolution view() {
        return new ForwardResolution(generateThemePath(VIEW));
    }


    @ValidationMethod(on={"handleForgottenPassword"})
    public void validate_handleForgottenPassword() {
        if (captcha != null && (!captcha.equals(getSessionAttribute(Constants.KAPTCHA_SESSION_KEY)))) {
            getContext().getValidationErrors().add("captcha", new LocalizableError("errors.CaptchaMismatch"));
        }
    }
    @Profiled(tag="CalendarActionBean_handleForgottenPassword", logFailuresSeparately = true)
    public Resolution handleForgottenPassword() {
        try {
            userService.performForgottenPasswordAction(username, email, getContext().getLocale());
            getContext().getMessages().add(new LocalizableMessage("foo.bar.blog.web.action.administration.ForgottenPasswordActionBean.messages.Success", new Object[] { username, email }));
        }
        catch(UserAndEmailCombinationDoesNotExistException e) {
            getContext().getValidationErrors().add("username", new LocalizableError("errors.UsernameEmailConbinationDoNotExists",
                    new Object[] {
                            username, //username
                            email //email
                    }));
            return new ForwardResolution(generateThemePath(VIEW));
        }
        catch(Exception e) {
            LOG.warn(e.toString(), e);
            getContext().getValidationErrors().add("username",
                    new LocalizableError("errors.UnableToSentFogottenPasswordEmail",
                            new Object[] {
                                    username,
                                    email
                            }));
            return new ForwardResolution(generateThemePath(VIEW));
        }
        return new RedirectResolution(ForgottenPasswordActionBean.class, "view");
    }




    // === accessor methods

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public void setCaptcha(String captcha) {
        this.captcha = captcha;
    }

    public String getCaptcha() {
        return captcha;
    }


    private UserService userService;

    @SpringBean("userService")
    public void setUserService(UserService userService) {
        this.userService = userService;
    }

}
